package cn.edu.hit.core.conf;

import cn.edu.hit.core.Document;

import java.util.Comparator;
import java.util.ServiceLoader;
import java.util.stream.StreamSupport;

public class ConfigureLoader {
    private volatile static ConfigureLoader uniqueInstance;

    private ConfigureLoader() {
        ServiceLoader<ConfigureProvider> loader = ServiceLoader.load(ConfigureProvider.class);
        StreamSupport.stream(loader.spliterator(), false)
                .sorted(Comparator.comparing(ConfigureProvider::priority).reversed())
                .forEach(configureProvider -> baseConfigure.putAll(configureProvider.loadConfigure()));
    }

    public static ConfigureLoader getInstance() {
        if (uniqueInstance == null) {
            synchronized (ConfigureLoader.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new ConfigureLoader();
                }
            }
        }
        return uniqueInstance;
    }

    private Document baseConfigure = new Document();

    public Document getBaseConfigure() {
        return baseConfigure;
    }
}
